/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

/**
 *
 * @author Mantas, Ramūnas, Vilius
 */
public class Stack {
    public static final int MAX_MEMORY = 16;
    public static final int MAX_BLOCKS = 16+1; // +1 kad butu galima realizuoti
    public static int memoryNeeded=0;
    
    private static String SP;

   
    public static String getSP() {
        return SP;
    }
    
    public static void setSP(String SP) {
        Stack.SP = SP;
    }
    
    public static void increaseSP(){
        Integer column = Integer.parseInt(SP.substring(2, 4));
        Integer row = Integer.parseInt(SP.substring(0, 2));
        if(column==Stack.MAX_BLOCKS-2){
            if(row == Stack.MAX_MEMORY-1){
                row=0;
            }else{
                row++;
            }
            SP = Processor.convertXtoXX(row)+Processor.convertXtoXX(0);
        }
        else{
            column++;
            SP = Processor.convertXtoXX(row)+Processor.convertXtoXX(column);
        }
    }
    
     public static void decreaseSP(){
        Integer column = Integer.parseInt(SP.substring(2, 4));
        Integer row = Integer.parseInt(SP.substring(0, 2));
        if(column==0){
            if(row == 0){
                row=Stack.MAX_MEMORY-1;
            }else{
                row--;
            }
            column=Stack.MAX_BLOCKS-2;
            SP = Processor.convertXtoXX(row)+Processor.convertXtoXX(column);
        }
        else{
            column--;
            SP = Processor.convertXtoXX(row)+Processor.convertXtoXX(column);
        }
    }
    /**
     * Metodas skirtas įrašyti duomenis į realią atminty pateikus virtualios
     * atminties adresą.
     * @param row
     * @param column
     * @param data
     */
    public static void setMemoryAt(int row, int column, String data){
        Integer realMemoryRow = PagingMechanism.getPagingTableRowStack(row);
        if(data.length()>4){
            data=data.substring(0, 4);
        }
        Memory.setMemoryAt(realMemoryRow, column, data);
    }
    /**
     * Metodas skirtas gauti atminties ląstelės turinį esantį realioje atmintyje
     * pateikus virtualios atminties adresą.
     * @param row
     * @param column
     * @return
     */
    public static String getMemoryFrom(int row, int column){
        Integer realMemoryRow = PagingMechanism.getPagingTableRowStack(row);
        if(realMemoryRow != null)
        {
            String tmp;
        
            tmp=Memory.getMemoryFrom(realMemoryRow, column);
            if(tmp!=null){
                return tmp;
            }
            else{
                return "";
            }
        }
        else
            return "";
    }
    public static void updateRowCount(){
        Integer SPTR = Processor.getSPTR();
        Integer i;
        Integer count=0;
        for(i=0; i<Stack.MAX_MEMORY; i++){
            if(Memory.getMemoryFrom(SPTR, i)!=null){
                count++;
            }
            else{
                memoryNeeded=count;
            }
        }

    }
}